package com.feeyin.radar.crm.controller;

import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.service.MyRecycleBinService;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

public class MyRecycleBinController extends MultiActionController
{
  private MyRecycleBinService myRecycleBinService;
  private String discardCustomer;

  public MyRecycleBinService getMyRecycleBinService()
  {
    return this.myRecycleBinService;
  }

  public void setMyRecycleBinService(MyRecycleBinService myRecycleBinService) {
    this.myRecycleBinService = myRecycleBinService;
  }

  public String getDiscardCustomer() {
    return this.discardCustomer;
  }

  public void setDiscardCustomer(String discardCustomer) {
    this.discardCustomer = discardCustomer;
  }

  public ModelAndView display(HttpServletRequest request, HttpServletResponse response)
  {
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    Map map = new HashMap();
    map.put("againResult", request.getParameter("againResult"));
    map.put("turnResult", request.getParameter("turnResult"));
    map.put("deleteResult", request.getParameter("deleteResult"));

    String sFlag = request.getParameter("sFlag");
    String customerName = "";
    if ((!StringUtils.isEmpty(sFlag)) && ("1".equals(sFlag))) {
      customerName = request.getParameter("customerName");
    }
    map.put("sFlag", sFlag);
    map.put("customerName", customerName);

    Page page = new Page();
    page.getPageList(this.myRecycleBinService.getPageDiscardCustomersCount(customerName, userId).intValue(), null, request);
    map.put("customers", this.myRecycleBinService.getPageDiscardCustomers(customerName, userId, page));

    return new ModelAndView(getDiscardCustomer(), map);
  }

  public ModelAndView againPickUpCustomer(HttpServletRequest request, HttpServletResponse response)
  {
    boolean result = false;
    int success = 0;
    Integer userId = (Integer)request.getSession().getAttribute("userId");

    String[] ids = request.getParameterValues("id");
    result = this.myRecycleBinService.againPickUpCustomer(ids, userId);
    if (result) success = 1;

    return new ModelAndView(new RedirectView("./myRecycleBin.do?action=display&againResult=" + success));
  }

  public ModelAndView turnToOpenCustomer(HttpServletRequest request, HttpServletResponse response)
  {
    boolean result = false;
    int success = 0;

    String[] ids = request.getParameterValues("id");
    result = this.myRecycleBinService.turnToOpenCustomer(ids);
    if (result) success = 1;

    return new ModelAndView(new RedirectView("./myRecycleBin.do?action=display&turnResult=" + success));
  }

  public ModelAndView deleteCustomer(HttpServletRequest request, HttpServletResponse response)
  {
    boolean result = false;
    int success = 0;
    Integer userId = (Integer)request.getSession().getAttribute("userId");

    String[] ids = request.getParameterValues("id");
    result = this.myRecycleBinService.deleteCustomer(ids, userId);
    if (result) success = 1;

    return new ModelAndView(new RedirectView("./myRecycleBin.do?action=display&deleteResult=" + success));
  }
}